package com.zoo.springbootinit.dasource;

import com.zoo.springbootinit.model.enums.SearchTypeEnum;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
public class DataSourceAccessorRegister {
    @Resource
    private PictureDataSourceAccessor pictureDataSourceAccessor;
    @Resource
    private UserDataSourceAccessor userDataSourceAccessor;
    @Resource
    private PostDataSourceAccessor postDataSourceAccessor;

    private Map<String, DataSourceAccessor<T>> dataSourceMap;

    @PostConstruct // 通过@PostConstruct注解，在bean初始化之后执行该方法
    public void init() {
        dataSourceMap = new HashMap() {{
            put(SearchTypeEnum.POST.getValue(), postDataSourceAccessor);
            put(SearchTypeEnum.USER.getValue(), userDataSourceAccessor);
            put(SearchTypeEnum.PICTURE.getValue(), pictureDataSourceAccessor);
        }};
    }

    public DataSourceAccessor getDataSourceByType(String type) {
        if (dataSourceMap != null) {
            return dataSourceMap.get(type);
        }
        return null;
    }
}
